﻿using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using Abp.Authorization;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Linq;
using Abp.Authorization.Users;
using Abp.Authorization.Roles;
using Abp.Domain.Uow;
using AbpProjectTemplate.Domain.Roles;

namespace AbpProjectTemplate.Domain.Users
{
    public class UserClaimsPrincipalFactory : AbpUserClaimsPrincipalFactory<User, Role>
    {
        public UserClaimsPrincipalFactory(AbpUserManager<Role, User> userManager, 
            AbpRoleManager<Role, User> roleManager, 
            IOptions<IdentityOptions> optionsAccessor, 
            IUnitOfWorkManager unitOfWorkManager) 
            : base(userManager, roleManager, optionsAccessor, unitOfWorkManager)
        {
        }

        public override async Task<ClaimsPrincipal> CreateAsync(User user)
        {
            var principal = await base.CreateAsync(user);
            principal.Identities.First().AddClaim(new Claim(AppConsts.ClaimTypes.UserTypeClaimType, user.UserType.ToString()));
            return principal;
        }
    }
}

